package com.mdd.admin.service.order.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.order.IOfflinePaymentService;
import com.mdd.admin.validate.order.OfflinePaymentCreateValidate;
import com.mdd.admin.validate.order.OfflinePaymentSearchValidate;
import com.mdd.admin.validate.order.OfflinePaymentUpdateValidate;
import com.mdd.admin.vo.order.OfflinePaymentDetailVo;
import com.mdd.admin.vo.order.OfflinePaymentListedVo;
import com.mdd.common.base.page.PageValidate;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.order.OfflinePayment;
import com.mdd.common.mapper.order.OfflinePaymentMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import javax.annotation.Resource;
import java.util.*;

/**
 * 订单还款计划线下支付记录实现类
 * @author LikeAdmin
 */
@Service
public class OfflinePaymentServiceImpl implements IOfflinePaymentService {

    @Resource
    OfflinePaymentMapper orderOfflinePaymentMapper;

    /**
     * 订单还款计划线下支付记录列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<OrderOfflinePaymentListedVo>
     */
    @Override
    public PageResult<OfflinePaymentListedVo> list(PageValidate pageValidate, OfflinePaymentSearchValidate searchValidate) {
        Integer page  = pageValidate.getPageNo();
        Integer limit = pageValidate.getPageSize();

        QueryWrapper<OfflinePayment> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        orderOfflinePaymentMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "=:repaymentPlanNo@repayment_plan_no:str",
            "=:paymentRent@payment_rent:str",
            "=:indemnifyforaloss:str",
            "=:payType@pay_type:str",
            "=:remarks:str",
        });

        IPage<OfflinePayment> iPage = orderOfflinePaymentMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<OfflinePaymentListedVo> list = new LinkedList<>();
        for(OfflinePayment item : iPage.getRecords()) {
            OfflinePaymentListedVo vo = new OfflinePaymentListedVo();
            BeanUtils.copyProperties(item, vo);
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 订单还款计划线下支付记录详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return OrderOfflinePayment
     */
    @Override
    public OfflinePaymentDetailVo detail(Long id) {
        OfflinePayment model = orderOfflinePaymentMapper.selectOne(
                new QueryWrapper<OfflinePayment>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "订单还款计划线下支付记录数据不存在");

        OfflinePaymentDetailVo vo = new OfflinePaymentDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 订单还款计划线下支付记录新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Transactional
    @Override
    public void add(OfflinePaymentCreateValidate createValidate) {
        OfflinePayment model = new OfflinePayment();
        model.setRepaymentPlanNo(createValidate.getRepaymentPlanNo());
        model.setPaymentRent(createValidate.getPaymentRent());
        model.setIndemnification(createValidate.getIndemnifyforaloss());
        model.setPayType(createValidate.getPayType());
        model.setRemarks(createValidate.getRemarks());
        orderOfflinePaymentMapper.insert(model);
    }

    /**
     * 订单还款计划线下支付记录编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Transactional
    @Override
    public void edit(OfflinePaymentUpdateValidate updateValidate) {
        OfflinePayment model = orderOfflinePaymentMapper.selectOne(
                new QueryWrapper<OfflinePayment>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "订单还款计划线下支付记录数据不存在!");

        model.setId(updateValidate.getId());
        model.setRepaymentPlanNo(updateValidate.getRepaymentPlanNo());
        model.setPaymentRent(updateValidate.getPaymentRent());
        model.setIndemnification(updateValidate.getIndemnifyforaloss());
        model.setPayType(updateValidate.getPayType());
        model.setRemarks(updateValidate.getRemarks());
        orderOfflinePaymentMapper.updateById(model);
    }

    /**
     * 订单还款计划线下支付记录删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Transactional
    @Override
    public void del(Long id) {
        OfflinePayment model = orderOfflinePaymentMapper.selectOne(
                new QueryWrapper<OfflinePayment>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "订单还款计划线下支付记录数据不存在!");

        orderOfflinePaymentMapper.delete(new QueryWrapper<OfflinePayment>().eq("id", id));
    }

}
